﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using DLL.Entities;
using DLL;

namespace MIS
{  
    public partial class MIS : Form
    {
        public MIS()
        {           
            InitializeComponent();
            defpic.Visible = true;
            picJericho.Visible = false;
            picDF21.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;
           
    
        }
        


  #region Missile Info
        List<Missile> missileInventory;
        Missile selectedMissile;
        int jerichoInventory;
        int dfInventory;
        int grinchInventory;
        int rim116Inventory;
        int aimInventory;
        int brahmosInventory;
        int starStreakInventory;
        int rim66Inventory;

  
        private void MIS_Load(object sender, EventArgs e)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Missile[]));
            string fileToRead = "../../../DLL/XML/Missile.xml";
            Missile[] missile = (Missile[])XMLHandler.READXML(fileToRead, serializer);
            missileInventory = missile.ToList();

            jerichoInventory = (from m in missile
                                where m.MissileName == "Jericho"
                                select m.MissileCount).FirstOrDefault();
            dfInventory = (from m in missile
                           where m.MissileName == "DF21"
                           select m.MissileCount).FirstOrDefault();
            grinchInventory = (from m in missile
                               where m.MissileName == "Grinch"
                               select m.MissileCount).FirstOrDefault();
            rim116Inventory = (from m in missile
                               where m.MissileName == "Rim-1600"
                               select m.MissileCount).FirstOrDefault();
            aimInventory = (from m in missile
                            where m.MissileName == "AIM-9L"
                            select m.MissileCount).FirstOrDefault();
            brahmosInventory = (from m in missile
                                where m.MissileName == "Brahmos"
                                select m.MissileCount).FirstOrDefault();
            starStreakInventory = (from m in missile
                                   where m.MissileName == "Starstreak"
                                   select m.MissileCount).FirstOrDefault();
            rim66Inventory = (from m in missile
                              where m.MissileName == "Rim-66"
                              select m.MissileCount).FirstOrDefault();
            
        }  
       
        private void btnJericho_Click(object sender, EventArgs e)
        {
            picJericho.Visible = true;
            defpic.Visible = false;
            picDF21.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                                where m.MissileName == "Jericho"
                                select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + jerichoInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }
        private void btnDf21_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picDF21.Visible = true;
            picJericho.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "DF21"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + dfInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
            
        }
        private void btnGrinch_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picGrinch.Visible = true;
            picDF21.Visible = false;
            picJericho.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "Grinch"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + grinchInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }

        private void btnRim116_Click(object sender, EventArgs e)
        {
          defpic.Visible = false;
          picRim116.Visible = true;
          picJericho.Visible = false;
          picGrinch.Visible = false;
          picAim9l.Visible = false;
          picBrahmos.Visible = false;
          picStarstreak.Visible = false;
          picRim66.Visible = false;

          selectedMissile = (from m in missileInventory
                             where m.MissileName == "Rim-1600"
                             select m).FirstOrDefault();
          txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
          txtDisplay.Text += Environment.NewLine;
          txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
          txtDisplay.Text += Environment.NewLine;
          txtDisplay.Text += "Missile Count: " + rim116Inventory;
          txtDisplay.Text += Environment.NewLine;
          txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
          txtDisplay.Text += Environment.NewLine;
          txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }

        private void btnAIM9L_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picAim9l.Visible = true;
            picJericho.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "AIM-9L"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + aimInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }

        private void btnBrahMos_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picBrahmos.Visible = true;
            picJericho.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picStarstreak.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "BrahMos"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + brahmosInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }

        private void btnStarstreak_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picStarstreak.Visible = true;
            picJericho.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picRim66.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "Starstreak"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + starStreakInventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }

        private void btnRim66_Click(object sender, EventArgs e)
        {
            defpic.Visible = false;
            picRim66.Visible = true;
            picJericho.Visible = false;
            picGrinch.Visible = false;
            picRim116.Visible = false;
            picAim9l.Visible = false;
            picBrahmos.Visible = false;
            picStarstreak.Visible = false;

            selectedMissile = (from m in missileInventory
                               where m.MissileName == "Rim-66"
                               select m).FirstOrDefault();
            txtDisplay.Text = "Missile Name: " + selectedMissile.MissileName;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Type: " + selectedMissile.MissileType;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Count: " + rim66Inventory;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Weight: " + selectedMissile.MissileWeight;
            txtDisplay.Text += Environment.NewLine;
            txtDisplay.Text += "Missile Range: " + selectedMissile.MissileRange;
        }
        #endregion 

        private void btnSelectMissile_Click(object sender, EventArgs e)
        {
            selectedMissile.MissileCommand = "Armed";

            if (selectedMissile.MissileName == "Jericho")
                selectedMissile.MissileCount = jerichoInventory;
            else if (selectedMissile.MissileName == "DF21")
                selectedMissile.MissileCount = dfInventory;
            else if (selectedMissile.MissileName == "Grinch")
                selectedMissile.MissileCount = grinchInventory;
            else if (selectedMissile.MissileName == "Rim-1600")
                selectedMissile.MissileCount = rim116Inventory;
            else if (selectedMissile.MissileName == "AIM-9L")
                selectedMissile.MissileCount = aimInventory;
            else if (selectedMissile.MissileName == "BrahMos")
                selectedMissile.MissileCount = brahmosInventory;
            else if (selectedMissile.MissileName == "Starstreak")
                selectedMissile.MissileCount = starStreakInventory;
            else if (selectedMissile.MissileName == "Rim-66")
                selectedMissile.MissileCount = rim66Inventory;

            XmlSerializer serializer = new XmlSerializer(typeof(Missile));
            string fileToWrite = "../../../MIS/MISEvent.xml";
            XMLHandler.WRITEXML(selectedMissile, fileToWrite, serializer);

            txtDisplay.Clear();
            defpic.Visible = true;
            Fired_Missile.Enabled = true;
       }

        private void Status_Writer_Tick(object sender, EventArgs e)
        {
            Status status = new Status();

            status.console = "MIS";
            status.status = Convert.ToString(DateTime.Now);

            XmlSerializer serializer = new XmlSerializer(typeof(Status));
            string fileToSave = "../../../MIS/MISStatus.xml";

            XMLHandler.WRITEXML(status, fileToSave, serializer);
        }

        private void Fired_Missile_Tick(object sender, EventArgs e)
        {
            if (File.Exists("../../../COM/STUBEVENT/MISFIRE.xml"))
            {            
                if (selectedMissile.MissileName == "Jericho")
                    jerichoInventory--;
                else if (selectedMissile.MissileName == "DF21")
                    dfInventory--;
                else if (selectedMissile.MissileName == "Grinch")
                    grinchInventory--;
                else if (selectedMissile.MissileName == "Rim-1600")
                    rim116Inventory--;
                else if (selectedMissile.MissileName == "AIM-9L")
                    aimInventory--;
                else if (selectedMissile.MissileName == "BrahMos")
                    brahmosInventory--;
                else if (selectedMissile.MissileName == "Starstreak")
                    starStreakInventory--;
                else if (selectedMissile.MissileName == "Rim-66")
                    rim66Inventory--;

                File.Move("../../../COM/STUBEVENT/MISFIRE.xml", "../../../MIS/" + DateTime.Now.Ticks + "MISFired");
            }
        }
    }
}
